<template>
  <div class="player-section">
    <!-- 控制按钮 -->
    <div class="control-buttons player-controls">
      <el-button-group>
        <el-button size="small" @click="stepBackward" title="上一帧">
          <el-icon><ArrowLeft /></el-icon><el-icon><ArrowLeft /></el-icon>
        </el-button>
        <el-button size="small" @click="rewind" title="快退">
          <el-icon><ArrowLeft /></el-icon>
        </el-button>
        <el-button size="small" @click="togglePlay" :icon="isPlaying ? VideoPause : VideoPlay" title="播放/暂停">
          {{ isPlaying ? '暂停' : '播放' }}
        </el-button>
        <el-button size="small" @click="forward" title="快进">
          <el-icon><ArrowRight /></el-icon>
        </el-button>
        <el-button size="small" @click="stepForward" title="下一帧">
          <el-icon><ArrowRight /></el-icon><el-icon><ArrowRight /></el-icon>
        </el-button>
      </el-button-group>
    </div>
    
    <!-- 播放器 - 固定宽高比 -->
    <div class="player-wrapper">
      <VideoPlayer
        ref="mainPlayer"
        :src="src"
        :autoplay="false"
        :controls="true"
        @ready="onPlayerReady"
        @timeupdate="onTimeUpdate"
        @error="onPlayerError"
        style="width: 100%; height: 100%;"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits } from 'vue'
import { ArrowLeft, ArrowRight, VideoPlay, VideoPause } from '@element-plus/icons-vue'
import VideoPlayer from '@/components/VideoPlayer.vue'

const props = defineProps({
  src: {
    type: String,
    default: ''
  },
  isPlaying: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['ready', 'timeupdate', 'error', 'togglePlay', 'stepForward', 'stepBackward', 'forward', 'rewind'])

const mainPlayer = ref(null)

const onPlayerReady = (event) => {
  emit('ready', event)
}

const onTimeUpdate = (event) => {
  emit('timeupdate', event)
}

const onPlayerError = (event) => {
  emit('error', event)
}

const togglePlay = () => {
  emit('togglePlay')
}

const stepForward = () => {
  emit('stepForward')
}

const stepBackward = () => {
  emit('stepBackward')
}

const forward = () => {
  emit('forward')
}

const rewind = () => {
  emit('rewind')
}
</script>

<style scoped>
.player-section {
  flex: 1;
  position: relative;
  background-color: var(--bg-color);
  border-radius: var(--border-radius-base);
  overflow: hidden;
  border-left: 4px solid var(--primary-color);
  box-sizing: border-box;
  min-height: 400px;
}

.player-wrapper {
  width: 100%;
  height: 400px;
  transition: all 0.3s ease;
}

.player-wrapper:hover {
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
}

.control-buttons.player-controls {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  padding: 8px 16px;
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 100;
  transition: all 0.3s ease;
  border-top: none;
}

.control-buttons.player-controls:hover {
  background-color: rgba(0, 0, 0, 0.8);
  transform: translateX(-50%) translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

.control-buttons.player-controls .el-button-group {
  display: flex;
  gap: 0;
}

.control-buttons.player-controls .el-button {
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #FFFFFF;
  border-radius: 0;
  transition: all 0.3s ease;
}

.control-buttons.player-controls .el-button:hover {
  background-color: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
  color: #FFFFFF;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.control-buttons.player-controls .el-button:first-child {
  border-radius: var(--border-radius-small) 0 0 var(--border-radius-small);
}

@media (max-width: 1200px) {
  .player-wrapper {
    height: 250px;
  }
}

@media (max-width: 768px) {
  .player-wrapper {
    height: 240px;
  }
}
</style>